package xxm.service;

import xxm.exception.ServiceException;
import xxm.exception.UtilityException;
import xxm.utility.BasicLogLevel;
import xxm.utility.BasicPathHelp;
import xxm.utility.BasicProperties;
import xxm.utility.BasicPropertiesManager;

/**
 * log filter interface implemnt
 * @author xxm
 *
 */
public class BasicLogFilterImpl implements BasicLogFilter {

	private int level;
	private String inifile;
	private String logname;
	

	/* (non-Javadoc)
	 * @see xxm.service.BasicLogFilter#init(java.lang.String)
	 */
	@Override
	public void init(String logFile) throws ServiceException {
		// TODO Auto-generated method stub
		try {
			logname = BasicPathHelp._basic_get_name(logFile);
			inifile = BasicPathHelp._basic_get_file_dir(logFile);
			inifile += "\\log.properties";
			
			if( (level = getLogLevel()) < 0 ) {
				level = BasicLogLevel.DEFAULT_LEVEL;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new ServiceException("BasicLogFilterImpl init", e );
		}
	}

	/* (non-Javadoc)
	 * @see xxm.service.BasicLogFilter#getLevel()
	 */
	@Override
	public int getLevel() throws ServiceException {
		// TODO Auto-generated method stub
		return this.level;
	}
	
	/**
	 * getLogLevel 
	 * @return
	 * @throws ServiceException
	 */
	protected int getLogLevel() throws ServiceException {
		try {
			BasicPropertiesManager.GetInstance().initProperties(this.inifile);
			String strLevel = BasicPropertiesManager.GetInstance().readValue(this.logname);
			if( strLevel == null ) 
			{
				return -1;
			}else {
				return Integer.parseInt(strLevel);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("BasicLogFilterImpl getLogLevel", e );
		}
	}

}
